package com.wny.activities;

import java.io.IOException;

import com.wny.R;
import com.wny.backend.SocketsClient;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainMenuActivity extends Activity implements LocationListener {
	Button publicUsersBtn;
	Button publicEventsBtn;
	Button myEventsBtn;
	Button friendsBtn;
	Button profileBtn;
	Button toJoinEventsBtn;
	Button closeUsers;
	OnClickListener publicUsersListener = null;
	OnClickListener publicEventsListener = null;
	OnClickListener myEventsListener = null;
	OnClickListener toJoinEventsListener = null;
	OnClickListener friendsListener = null;
	OnClickListener profileListener = null;
	OnClickListener peopleNearMe = null;
	String getUsers = "get=user\n";
	String getEvents = "get=event\n";

	int uid;
	String getToJoin = "get=eventList&uid=";
	String getMyEvents = "get=event&organizerID=";
	String getFriends = "get=friend&uid1=";
	private Location myLocation;
	LocationManager myProvider;
	private double radius = 1;
	public void onResume()
	{
		super.onResume();
		myProvider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
	}
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		Bundle extras = getIntent().getExtras();
		myProvider = (LocationManager) getSystemService(LOCATION_SERVICE);
		if (extras != null) {
			uid = extras.getInt("uid");
		}
		publicUsersListener = new OnClickListener() {
			public void onClick(View v) {
				Intent publicUserIntent = new Intent(MainMenuActivity.this,
						DisplayListActivity.class);
				setTitle("Public Users");
				publicUserIntent.putExtra("request", getUsers);
				publicUserIntent.putExtra("requestType", "user");
				publicUserIntent.putExtra("uid", uid);
				startActivity(publicUserIntent);
			}
		};
		
		publicEventsListener = new OnClickListener() {
			public void onClick(View v) {
				Intent publicEventIntent = new Intent(MainMenuActivity.this,
						DisplayListActivity.class);
				setTitle("Public Events");
				publicEventIntent.putExtra("request", getEvents);
				publicEventIntent.putExtra("requestType", "publicEvents");
				publicEventIntent.putExtra("uid", uid);
				startActivity(publicEventIntent);
			}
		};

		myEventsListener = new OnClickListener() {
			public void onClick(View v) {
				Intent myEventIntent = new Intent(MainMenuActivity.this,
						DisplayListActivity.class);
				setTitle("My Events");
				myEventIntent.putExtra("request", getMyEvents+String.valueOf(uid)+"\n");
				myEventIntent.putExtra("requestType", "myEvents");
				myEventIntent.putExtra("uid", uid);
				startActivity(myEventIntent);
			}
		};
		
		toJoinEventsListener = new OnClickListener() {
			public void onClick(View v) {
				Intent toJoinEventIntent = new Intent(MainMenuActivity.this,
						DisplayListActivity.class);
				setTitle("To Join Events");
				toJoinEventIntent.putExtra("request", getToJoin+String.valueOf(uid)+"\n");
				toJoinEventIntent.putExtra("requestType", "toJoinEvents");
				toJoinEventIntent.putExtra("uid", uid);
				startActivity(toJoinEventIntent);
			}
		};
		
		friendsListener = new OnClickListener() {
			public void onClick(View v) {
				Intent friendIntent = new Intent(MainMenuActivity.this,
						DisplayListActivity.class);
				setTitle("My Friends");
				friendIntent.putExtra("request", getFriends+String.valueOf(uid)+"\n");
				friendIntent.putExtra("requestType", "user");
				friendIntent.putExtra("uid", uid);
				startActivity(friendIntent);
			}
		};

		profileListener = new OnClickListener() {
			public void onClick(View v) {
				Intent profileIntent = new Intent(MainMenuActivity.this,
						UserActivity.class);
				setTitle("My Profile");
				profileIntent.putExtra("otherUid",uid);
				profileIntent.putExtra("friends",true);
				profileIntent.putExtra("uid", uid);
				startActivity(profileIntent);
			}
		};
		peopleNearMe = new OnClickListener() {
			
			public void onClick(View V) {
				StringBuilder toSend = new StringBuilder("get=user&location=");
				double latitude;
				double longitude;
				//update location if not null
				if(myLocation==null)
				{
					latitude = 0;
					longitude = 0;
				}
				else
				{
					latitude = myLocation.getLatitude();
					longitude = myLocation.getLongitude();
				}
				//add the # to serve as a delimeter when sending data to server
				toSend.append(latitude);
				toSend.append("#");
				toSend.append(longitude);
				toSend.append("#");
				toSend.append(radius);
				toSend.append("\n");
				Intent displayIntent = new Intent(MainMenuActivity.this, DisplayListActivity.class);
				displayIntent.putExtra("requestType", "user");
				displayIntent.putExtra("request", toSend.toString());
				displayIntent.putExtra("uid", uid);
				startActivity(displayIntent);
			}
		};
		publicUsersBtn = (Button) findViewById(R.id.publicUsers);
		publicUsersBtn.setOnClickListener(publicUsersListener);

		publicEventsBtn = (Button) findViewById(R.id.publicEvents);
		publicEventsBtn.setOnClickListener(publicEventsListener);

		myEventsBtn = (Button) findViewById(R.id.myEvents);
		myEventsBtn.setOnClickListener(myEventsListener);

		toJoinEventsBtn = (Button) findViewById(R.id.tojoinEvents);
		toJoinEventsBtn.setOnClickListener(toJoinEventsListener);

		friendsBtn = (Button) findViewById(R.id.friends);
		friendsBtn.setOnClickListener(friendsListener);

		profileBtn = (Button) findViewById(R.id.profile);
		profileBtn.setOnClickListener(profileListener);
		
		closeUsers = (Button) findViewById(R.id.closeUsers);
		closeUsers.setOnClickListener(peopleNearMe);
		myLocation = null;
	}
	//updates user GPS coordinates
	public void onLocationChanged(Location location)
	{
		// TODO Auto-generated method stub
		SocketsClient myComm = SocketsClient.getSocket();
		this.myLocation = location;
		try {
			myComm.sendNonblocking(("loc&"+location.getLatitude()+"&"+location.getLongitude()+"&"+this.uid+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//notifies user GPS disables
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("The system GPS service has been disabled.", 5000);
	}

	//notifies user GPS enabled
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("GPS is now avaliable", 4000);

	}

	//notifies user of any change in the status of the GPS
	public void onStatusChanged(String location_provider, int status, Bundle otherVariables) {
		// TODO Auto-generated method stub
		switch (status)
		{
		case LocationProvider.OUT_OF_SERVICE:
			displayWithToast("The GPS is out of service.", 5000);
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			displayWithToast("The GPS is temporarially unavaliable", 5000);
		case LocationProvider.AVAILABLE:
			displayWithToast("GPS is now avaliable", 5000);
		}

	}
	private void displayWithToast(String toShow, int duration)
	{
		Toast temp = Toast.makeText(this, toShow, duration);
		temp.setGravity(Gravity.CENTER, 0, 0);
		temp.show();
	}
}
